var a = require("../../api.js"), t = getApp(), e = "", o = "";

Page({
    data: {
        address: null,
        offline: 1,
        payment: -1,
        show_payment: !1,
        distance: -1
    },
    onLoad: function(a) {
        t.pageOnLoad(this, a), wx.removeStorageSync("input_data");
        var e, o = this, i = a.goods_info, s = JSON.parse(i);
        // e = 3 == s.deliver_type || 1 == s.deliver_type ? 1 : 2,
         o.setData({
            options: a,
            type: s.type,
            // offline: e,
            parent_id: s.parent_id ? s.parent_id : 0
        });
        this.themeColor();
    },
    themeColor: function() {
        var cl = wx.getStorageSync('_navigation_bar_color').themeColor;
        var cl_b = !cl || cl == '#ffffff' ? '#ff4544' : cl;
        var color_rgb = this.hexToRgb(cl_b);
        var back_g = 'rgba(' + color_rgb.r + ',' + color_rgb.g + ',' + color_rgb.b + ',.2)'
        this.setData({
          all_color: cl ? cl : '#ffffff',
          back_g: back_g
          // back_end: back_end
        });
        wx.setNavigationBarColor({
          frontColor: cl != '#ffffff' ? '#ffffff' : '#000000',
          backgroundColor: cl,
          animation: {
            duration: 0,
            timingFunc: 'easeIn'
          }
        })
      },
      hexToRgb: function(hex) {
        var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
        return result ? {
          r: parseInt(result[1], 16),
          g: parseInt(result[2], 16),
          b: parseInt(result[3], 16)
        } : null;
      },
    onReady: function() {},
    onShow: function() {
        t.pageOnShow(this);
        var a = this, e = wx.getStorageSync("picker_address");
        e && (a.setData({
            address: e,
            name: e.name,
            mobile: e.mobile
        }), wx.removeStorageSync("picker_address"), a.getInputData());

        var s = wx.getStorageSync("picker_order");
        if (s ) {
            var shop = s;
            if (s.peisong_type) {
            shop.peisong_type = s.peisong_type;
            }
            var distance = s.distance;
            var peisong_price = s.peisong_price;
            var res = a.data.res;
            res.peisong_price = s.peisong_price;
            a.setData({
                shop: shop,
                distance: distance,
                peisong_price: peisong_price - 0,
                send_type: s.peisong_type ? s.peisong_type : a.data.send_type,
                res: res
            })
            wx.removeStorageSync('picker_order')
            var status = a.data.send_type.is_ziti ? 1 : (a.data.send_type.is_daojia ? 2 :3)
            this.getOffline(status);
        }else {
            a.getOrderData(a.data.options)
        }
    },
    onHide: function() {
        t.pageOnHide(this), this.getInputData();
    },
    onUnload: function() {
        t.pageOnUnload(this), wx.removeStorageSync("input_data");
    },
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    getOrderData: function(i) {
        var s = this, n = "";
        s.data.address && s.data.address.id && (n = s.data.address.id), i.goods_info && (wx.showLoading({
            title: "正在加载",
            mask: !0
        }), t.request({
            url: a.group.submit_preview,
            data: {
                goods_info: i.goods_info,
                group_id: i.group_id,
                address_id: n,
                type: s.data.type,
                longitude: e,
                latitude: o
            },
            success: function(a) {
                if (wx.hideLoading(), 0 == a.code) {
                    a.data.peisong_price = s.data.shop && s.data.shop.peisong_price ? s.data.shop.peisong_price : a.data.peisong_price;
                    var o = wx.getStorageSync("input_data");
                    wx.removeStorageSync("input_data"), o || (o = {
                        address: a.data.address,
                        name: a.data.offline_address ? a.data.offline_address.name : "",
                        mobile: a.data.offline_address ? a.data.offline_address.mobile : ""
                    }, a.data.pay_type_list.length > 0 && (o.payment = a.data.pay_type_list[0].payment, 
                    a.data.pay_type_list.length > 1 && (o.payment = -1))), o.total_price = a.data.total_price, 
                    o.goods_list = a.data.list, o.goods_info = a.data.goods_info, o.express_price = e, 
                    o.send_type = s.data.send_type ? s.data.send_type : a.data.send_type, 
                    o.colonel = a.data.colonel, 
                      o.pay_type_list = a.data.pay_type_list, o.shop_list = a.data.shop_list, 
                      o.shop = s.data.shop ? s.data.shop : a.data.shop, 
                      o.res = a.data, 
                    o.is_area = a.data.is_area, 
                    o.offline=s.data.shop ? s.data.offline : a.data.offline, 
                    o.is_peisong= s.data.is_peisong ? s.data.is_peisong : a.data.is_peisong, 
                    // o.peisong_price = peisong_price, 
                    s.setData(o), s.getInputData();
                    var status = o.send_type.is_ziti ? 1 : (o.send_type.is_daojia ? 2 :3)
                    s.getOffline(status);
                }
                1 == a.code && wx.showModal({
                    title: "提示",
                    content: a.msg,
                    showCancel: !1,
                    confirmText: "返回",
                    success: function(a) {
                        a.confirm && wx.navigateBack({
                            delta: 1
                        });
                    }
                });
            },
            complete: function () {      
                if (s.data.shop && s.data.distance <0) {
                    wx.getLocation({
                      success: function (res) {
                        var dis = s.distance(res.latitude, res.longitude, s.data.shop.latitude, s.data.shop.longitude)
                        s.setData({
                          distance: dis
                        })
                      }
                    });
                  }
            }
        }));
    },
    distance: function (la1, lo1, la2, lo2) {

        var La1 = la1 * Math.PI / 180.0;
    
        var La2 = la2 * Math.PI / 180.0;
    
        var La3 = La1 - La2;
    
        var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
    
        var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
    
        s = s * 6378.137; //地球半径
    
        s = Math.round(s * 10000) / 10000;
        return s.toFixed(2) +'km';
      },
    bindkeyinput: function(a) {
        this.setData({
            content: a.detail.value
        });
    },
    orderSubmit: function(a) {
        var t = this, e = {}, o = t.data.offline, is_peisong = t.data.is_peisong,
        peisong_price = t.data.peisong_price;
        e.offline = o;
        if ( 1 == o) {
            if (!t.data.address || !t.data.address.id) return void wx.showToast({
                title: "请选择收货地址",
                image: "/images/icon-warning.png"
            });
            e.address_id = t.data.address.id;
        } else {
            e.shop_id = t.data.shop.id;
          
            if(is_peisong == 1){
                if (!t.data.address || !t.data.address.id) return void wx.showToast({
                    title: "请选择收货地址",
                    image: "/images/icon-warning.png"
                });
                e.address_id = t.data.address.id;
            }else{
                if (e.address_name = t.data.name, e.address_mobile = t.data.mobile, !t.data.shop.id) return void wx.showToast({
                    title: "请选择社区",
                    image: "/images/icon-warning.png"
                });
                if (!e.address_name || void 0 == e.address_name) return void wx.showToast({
                    title: "请填写收货人",
                    image: "/images/icon-warning.png"
                });
                if (!e.address_mobile || void 0 == e.address_mobile) return void wx.showToast({
                    title: "请填写联系方式",
                    image: "/images/icon-warning.png"
                });
                if (!/^\+?\d[\d -]{8,12}\d/.test(e.address_mobile)) return void wx.showModal({
                    title: "提示",
                    content: "手机号格式不正确"
                });
            }
           
        }
        return -1 == t.data.payment ? (t.setData({
            show_payment: !0
        }), !1) : (t.data.goods_info && (e.goods_info = JSON.stringify(t.data.goods_info)), 
        t.data.picker_coupon && (e.user_coupon_id = t.data.picker_coupon.user_coupon_id), 
        t.data.content && (e.content = t.data.content), t.data.type && (e.type = t.data.type), 
        t.data.parent_id && (e.parent_id = t.data.parent_id), e.payment = t.data.payment, 
        e.formId = a.detail.formId, e.is_peisong = is_peisong, e.peisong_price = peisong_price, void t.order_submit(e, "pt"));
    },
    KeyName: function(a) {
        this.setData({
            name: a.detail.value
        });
    },
    KeyMobile: function(a) {
        this.setData({
            mobile: a.detail.value
        });
    },
    getOffline: function(a) {
        var t = this, e = typeof(a) == 'string' || typeof(a) == 'number' ? a : a.target.dataset.index;
       
        if (1 == e) {
          
            var i = (parseFloat(t.data.res.total_price - t.data.res.colonel > 0 ? t.data.res.total_price - t.data.res.colonel : .01)).toFixed(2);
            this.setData({
                offline: 2,
                is_peisong: 0,
                express_price: 0,
                peisong_price: 0,
                total_price_1: i
            });
        } else if (2 == e) {
           
            var i1 = parseFloat(t.data.res.total_price - t.data.res.colonel > 0 ? t.data.res.total_price - t.data.res.colonel : .01) + parseFloat(t.data.res.peisong_price ? t.data.res.peisong_price : 0);
           
            this.setData({
                offline: 2,
                is_peisong: 1,
                express_price: 0,
                peisong_price: t.data.res.peisong_price,
                total_price_1: i1.toFixed(2),
            });
        }else{
           
            var i2 = (parseFloat(t.data.res.total_price - t.data.res.colonel > 0 ? t.data.res.total_price - t.data.res.colonel : .01) + t.data.res.express_price).toFixed(2);
            this.setData({
                offline: 1,
                is_peisong: 0,
                express_price: t.data.res.express_price,
                peisong_price: 0,
                total_price_1: i2
            });
        }

    },
    showShop: function(a) {
        var t = this;
        t.getInputData(), t.dingwei(), t.data.shop_list && t.data.shop_list.length >= 1 && t.setData({
            show_shop: !0
        });
    },
    dingwei: function() {
        var a = this;
        wx.chooseLocation({
            success: function(t) {
                e = t.longitude, o = t.latitude, a.setData({
                    location: t.address
                });
            },
            fail: function(e) {
                t.getauth({
                    content: "需要获取您的地理位置授权，请到小程序设置中打开授权",
                    success: function(t) {
                        t && (t.authSetting["scope.userLocation"] ? a.dingwei() : wx.showToast({
                            title: "您取消了授权",
                            image: "/images/icon-warning.png"
                        }));
                    }
                });
            }
        });
    },
    pickShop: function(a) {
        var t = this, e = wx.getStorageSync("input_data"), o = a.currentTarget.dataset.index;
        e.show_shop = !1, e.shop = "-1" != o && -1 != o && t.data.shop_list[o], t.setData(e);
    },
    showPayment: function() {
        this.setData({
            show_payment: !0
        });
    },
    payPicker: function(a) {
        var t = a.currentTarget.dataset.index;
        this.setData({
            payment: t,
            show_payment: !1
        });
    },
    payClose: function() {
        this.setData({
            show_payment: !1
        });
    },
    getInputData: function() {
        var a = this, t = {
            address: a.data.address,
            name: a.data.name,
            mobile: a.data.mobile,
            payment: a.data.payment,
            content: a.data.content
        };
        wx.setStorageSync("input_data", t);
    }
});